﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmCapNhatDmPhongKham : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler dmPhongKhamEvent;
        ThietLapMaBusiness thietLapMaBusiness = new ThietLapMaBusiness();
        int State = 0;
        DmPhongKhamEntity dmPhongKhamEntity = null;
        DmPhongKhamBusiness dmPhongKhamBusiness = new DmPhongKhamBusiness();
        DmKhoaBusiness dmKhoaBusiness = new DmKhoaBusiness();

        public frmCapNhatDmPhongKham(int state)
        {
            InitializeComponent();
            LoadDmKhoa();
            this.State = state;
        }

        public frmCapNhatDmPhongKham(int state, DmPhongKhamEntity obj)
        {
            InitializeComponent();

            LoadDmKhoa();
            this.State = state;
            dmPhongKhamEntity = obj;

            txtMaPhongKham.Text = dmPhongKhamEntity.MaPhongKham;
            lkDmKhoa.EditValue = dmPhongKhamEntity.MaKhoa;
            txtTenPhongKham.Text = dmPhongKhamEntity.TenPhongKham;
            txtDiaChi.Text = dmPhongKhamEntity.DiaChi;
            txtDienThoai.Text = dmPhongKhamEntity.DienThoai;
            txtMoTa.Text = dmPhongKhamEntity.MoTa;
            chkTrangThaiSuDung.Checked = dmPhongKhamEntity.TrangThaiSuDung;
            txtMaPhongKham.Enabled = false;
        }

        void LoadDmKhoa()
        {
            lkDmKhoa.Properties.DataSource = dmKhoaBusiness.GetDmKhoaList();
            lkDmKhoa.Properties.DisplayMember = "TenKhoa";
            lkDmKhoa.Properties.ValueMember = "MaKhoa";
        }

        void LoadData(object sender, EventArgs e)
        {
            LoadDmKhoa();
        }

        private void frmCapnhatDMPhongkham_Load(object sender, EventArgs e)
        {
            LoadDmKhoa();
        }

        private void frmCapnhatDMPhongkham_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (Validation.ValidLookUpEdit(lkDmKhoa, "Khoa") == false) return;
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidSpaceTextBoxKey(txtMaPhongKham, "Mã phòng khám") == false) return;

            dmPhongKhamEntity = new DmPhongKhamEntity()
            {
                MaKhoa = lkDmKhoa.EditValue.ToString(),
                MaPhongKham = txtMaPhongKham.Text,
                TenPhongKham = txtTenPhongKham.Text.Trim(),
                DiaChi = txtDiaChi.Text.Trim(),
                DienThoai = txtDienThoai.Text.Trim(),
                MoTa = txtMoTa.Text.Trim(),
                TrangThaiSuDung = (bool)chkTrangThaiSuDung.Checked
            };

            if (State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmPhongKhamBusiness.GetDmPhongKham(txtMaPhongKham.Text.Trim()) != null)
                    if (Validation.ValidPrimaryKey(txtMaPhongKham, "Mã phòng khám") == false) return;

                dmPhongKhamBusiness.InsertDmPhongKham(dmPhongKhamEntity);
                Validation.ClearControl(this);
                txtMaPhongKham.Text = thietLapMaBusiness.GetThietLapMa(ThietLapMaEntity.MaPhongKham);
                txtTenPhongKham.Focus();
            }
            else
            {
                dmPhongKhamBusiness.EditDmPhongKham(dmPhongKhamEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (dmPhongKhamEvent != null)
                dmPhongKhamEvent(sender, e);
        }

        private void btnThemKhoa_Click(object sender, EventArgs e)
        {
            var frm = new frmCapNhatDmKhoa((int)ConstantManagerClinic.StateInsertOrUpdate.Insert);
            frm.dmKhoaEvent += new EventHandler(LoadData);
            frm.ShowDialog();
        }

    }
}